package edu.sandau.app.basic.service.impl;

import edu.sandau.app.basic.domain.SysUser;
import edu.sandau.app.basic.domain.SysUserExample;
import edu.sandau.app.basic.mapper.SysUserMapper;
import edu.sandau.app.basic.service.SysUserService;
import edu.sandau.app.basic.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    
    @Autowired
    private SysUserMapper sysUserMapper;
    
    @Override
    public List<SysUser> getAllUsers() {
        SysUserExample example = new SysUserExample();
        List<SysUser> users = sysUserMapper.selectByExample(example);
        // 隐藏密码信息
        for (SysUser user : users) {
            user.setPassword("******");
        }
        return users;
    }
    
    @Override
    public SysUser getUserById(Long id) {
        SysUser user = sysUserMapper.selectByPrimaryKey(id);
        if (user != null) {
            // 隐藏密码信息
            user.setPassword("******");
        }
        return user;
    }
    
    @Override
    @Transactional
    public int addUser(SysUser user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        if (user.getStatus() == null) {
            user.setStatus("0"); // 默认状态为正常
        }
        if (user.getUserType() == null) {
            user.setUserType("1"); // 默认为普通用户
        }
        return sysUserMapper.insertSelective(user);
    }
    
    @Override
    @Transactional
    public int updateUser(SysUser user) {
        user.setUpdateTime(new Date());
        return sysUserMapper.updateByPrimaryKeySelective(user);
    }
    
    @Override
    @Transactional
    public int deleteUser(Long id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public List<SysUser> getUsersByUsername(String username) {
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUsernameLike("%" + username + "%");
        List<SysUser> users = sysUserMapper.selectByExample(example);
        // 隐藏密码信息
        for (SysUser user : users) {
            user.setPassword("******");
        }
        return users;
    }
    
    @Override
    public List<SysUser> getUsersByType(String userType) {
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUserTypeEqualTo(userType);
        List<SysUser> users = sysUserMapper.selectByExample(example);
        // 隐藏密码信息
        for (SysUser user : users) {
            user.setPassword("******");
        }
        return users;
    }
    
    @Override
    public boolean checkUsernameExists(String username) {
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        return sysUserMapper.countByExample(example) > 0;
    }
    
    @Override
    public boolean verifyPassword(String username, String password) {
        try {
            SysUserExample example = new SysUserExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<SysUser> users = sysUserMapper.selectByExample(example);
            
            if (users != null && !users.isEmpty()) {
                SysUser user = users.get(0);
                String storedPassword = user.getPassword();
                
                // 检查存储的密码是否为加密格式
                if (PasswordUtils.isPlainText(storedPassword)) {
                    // 如果是明文密码（旧数据），直接比较
                    return password != null && password.equals(storedPassword);
                } else {
                    // 如果是加密密码，使用BCrypt验证
                    return PasswordUtils.matches(password, storedPassword);
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }
}

